package proxy;

import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.InvocationHandler;
import java.lang.reflect.Method;
@Slf4j
public class MockServiceProxy implements InvocationHandler {
    public Object invoke(Object proxy, Method method, Object[] args) throws Throwable{
        Class<?> classType= method.getReturnType();
        log.info("mock的对象是:" + method.getDeclaringClass().getName() + ", 调用的模拟方法是:" + method.getName());
        return selectByClassType(classType);
    }

    /**
     * Mock对象,根据调用函数的返回值类型来返回结果
     * @param classType
     * @return
     */
    private Object selectByClassType(Class<?> classType) {
        if(classType.isPrimitive()){
            if(classType == int.class)return 0;
            else if(classType == short.class)return (short)0;
            else if(classType == long.class)return 0l;
            else if(classType == byte.class)return (byte)0;
            else if(classType == double.class)return 0.0;
            else if(classType == float.class)return 0.0f;
        }
        return null;
    }
}
